package com.ddwl.goods.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.goods.service.AcademyService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.AcademyListVo;
import com.ddwl.schema.vo.goods.AcademyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 商学院文章管理
 * @author wenbin
 */
@Api(tags = "商学院文章管理")
@RequestMapping("Academy")
@RestController
public class AcademyController extends BaseController {

    @Resource
    private AcademyService academyService;


    @PostMapping("academyUnApproveList")
    @ApiOperation("查询商学院文章待审核数量")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<Integer> academyUnApproveList(@RequestBody QueryAcademyBo pageBo) {
        return RestVo.ok(academyService.academyUnApproveList(pageBo));
    }

    @PostMapping("academyListConsole")
    @ApiOperation("查询商学院文章列表(后台)")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<AcademyVo>> academyListConsole(@RequestBody PageBo<QueryAcademyBo> pageBo) {
        return RestVo.ok(academyService.academyList(pageBo));
    }

    @GetMapping("academyInfoConsole")
    @ApiOperation("查询商学院文章详情(后台)")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<AcademyVo> academyInfoConsole(@ApiParam(value = "文章ID",required = true) String id) {
        return RestVo.ok(academyService.academyDetail(id));
    }

    @PostMapping("academyList")
    @ApiOperation("按照分类查询商学院文章列表 app")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo<PageVo<AcademyVo>> academyList(@RequestBody PageBo<QueryAcademyBo> pageBo) {
        return RestVo.ok(academyService.academyPageForApp(pageBo));
    }

    @PostMapping("/searchAcademy")
    @ApiOperation("商学院文章列表搜索 app")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo<PageVo<AcademyVo>> searchAcademy(@RequestBody  PageBo<AcademySearchBo> pageBo) {
        return RestVo.ok(academyService.searchAcademy(pageBo));
    }

    @GetMapping("academyDetail/{academyId}")
    @ApiOperation("查询商学院文章明细 app")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo<AcademyVo> academyDetail(@PathVariable String academyId) {
        return RestVo.ok(academyService.academyDetail(academyId,getUserId()));
    }

    @GetMapping("academyDetailForH5/{academyId}")
    @ApiOperation("查询商学院文章明细 h5")
    public RestVo<AcademyVo> academyDetailForH5(@PathVariable String academyId) {
        return RestVo.ok(academyService.academyDetail(academyId,getUserId()));
    }

    @PostMapping("/createAcademy")
    @ApiOperation("创建文章")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo createAcademy(@Valid @RequestBody AcademyBo bo) {
        academyService.createAcademy(bo,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/publishAcademy")
    @ApiOperation("app发表文章")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo publishAcademy(@Valid @RequestBody SelfAcademyAddBo bo) throws Exception {
        academyService.publishAcademy(bo,getUserId(),getUserNameNew());
        return RestVo.ok();
    }

    @PostMapping("/saveDraft")
    @ApiOperation("app文章保存草稿")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo saveDraft(@Valid @RequestBody SelfAcademyAddBo bo) {
        academyService.saveDraft(bo,getUserId(),getUserNameNew());
        return RestVo.ok();
    }

    @PostMapping("/reEditAcademy")
    @ApiOperation("app修改文章")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo publishAcademy(@Valid @RequestBody SelfAcademyUpdateBo bo) {
        academyService.reEditAcademy(bo,getUserId(),getUserNameNew());
        return RestVo.ok();
    }

    @PostMapping("/myAcademyPage")
    @ApiOperation("查询我的商学院文章列表 app")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo<PageVo<AcademyVo>> myAcademyPage(@RequestBody  PageBo<SelfAcademyQueryBo> pageBo) {
        pageBo.getParam().setCreatedUserId(getUserId());
        return RestVo.ok(academyService.myAcademyPage(pageBo));
    }

    @PostMapping("/cancelReview/{id}")
    @ApiOperation("我的文章取消审核 app")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo cancelReview(@PathVariable("id") String id) {
        academyService.cancelReview(id);
        return RestVo.ok();
    }

    @PostMapping("/updateAcademy")
    @ApiOperation("修改文章")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo updateAcademy(@Valid @RequestBody AcademyBo bo) {
        academyService.updateAcademy(bo,getUserId());
        return RestVo.ok();
    }

    @DeleteMapping("deleteAcademy/{id}")
    @ApiOperation("删除文章")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.FENXIAO})
    public RestVo deleteAcademy(@PathVariable("id") String id) {
        academyService.deleteAcademy(id,getUserId());
        return RestVo.ok();
    }

    @GetMapping("getAcademyTotal")
    @ApiOperation("获取app整页文章")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.FENXIAO})
    public RestVo<List<AcademyListVo>> getAcademyTotal() {
        List<AcademyListVo> list=academyService.getAcademyTotal();
        return RestVo.ok(list);
    }



}
